<?php
/**
订单管理  商品订单|物业订单|停车费
 */

namespace App\Http\Controllers\Property;


use App\Common\json;
use App\Common\SmsClient;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\sms;


class OrderController extends  Controller{


    //获取 1商品订单|2物业费|3停车费
    public function Orderlist(Request $request,$type){
        if($request ->isMethod('post')){
            $user_id        = $request ->input('user_id');
            $data           = DB::table('order')
                            ->leftjoin('goods','order.order_goods_id','=','goods.goods_id')
                            ->where('user_order',$user_id)
                            ->where('order_type',$type)
                            ->get();
            if($data == true){
                return json::json('success', '获取商品订单成功', $data);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
        }

        //商品订单详情
    public function OrderDetails(Request $request){
        if($request ->isMethod('post')){
            $order_id       = $request ->input('order_id');
            $data           = DB::table('order')
                            ->leftjoin('goods','order.order_goods_id','=','goods.goods_id')
                            ->leftjoin('goodstype','goods.type_goods','=','goodstype.type_id')
                            ->leftjoin('user','order.user_order','=','user.user_id')
                            ->leftjoin('address','order.order_address','=','address.address_id')
                            ->leftjoin('ordercontent','order.order_id','=','ordercontent.order_ordercontent')
                            ->where('order_id',$order_id)
                            ->get();
            if($data == true){
                foreach ($data as $value){
                    $timeend               = $value->activity_end;
                    $dat                  =  date('Y-m-d H:i:s');
                    $timesend                 = strtotime($timeend)-strtotime($dat);
                    $type      = $value ->typename;
                    $goods_id  = $value ->goods_id;
                    $order     = $value ->order;
                    $order_status  = $value ->order_status;
                    $order_create_time = $value ->order_create_time;
                    $nickname          = $value->nickname;
                    $province           = $value ->province;
                    $address            = $value ->address;
                    $gather             = $province.$address;
                    $phone              = $value ->phone;
                    $goodsname          = $value ->order_goodsname;
                    $quantity           = $value ->quantity;
                    $sum                = $value ->sum;
                    $ordercontent       = $value ->ordercontent;
                    $quality            = $value ->quality;
                    $speed              = $value ->speed;
                    $goodsimage         = $value ->goodsimage;
                    $da        = array('time'=>$timesend*1000,'type'=>$type,'goods_id'=>$goods_id,'order'=> $order,'order_status'=>$order_status,'order_create_time'=>$order_create_time,
                        'nickname'=>$nickname,'phone'=>$phone,'address'=>$gather,'goodsname'=>$goodsname,'quantity'=>$quantity,'sum'=>$sum,
                        'ordercontent'=>$ordercontent,'quality'=>$quality,'speed'=>$speed,'goodsimage'=>$goodsimage);
                }
                return json::json('success', '获取订单详情成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }


    //订单评论
    public function OrderContent(Request $request)
    {
        if ($request->isMethod('post')) {
            $order_id = $request->input('order_id');
            $user_id = $request->input('user_id');
            $ordercontents = $request->input('content');
            $quality = $request->input('quality');
            $speed = $request->input('speed');
            $time = date('Y-m-d H:i:s');
            $ordercontent = DB::table('ordercontent')
                ->where('user_ordercontent', $user_id)
                ->where('order_ordercontent', $order_id)
                ->get();
            if ($ordercontent == true) {
                return json::json('failure', '您已经评论过', json::value(1));
            } else {
                $select = DB::table('order')
                    ->where('order_id', $order_id)
                    ->where('order_status', 0)
                    ->get();
                if ($select == true) {
                    $update = DB::table('ordercontent')
                        ->insert([
                            'user_ordercontent' => $user_id,
                            'order_ordercontent' => $order_id,
                            'ordercontent' => $ordercontents,
                            'quality' => $quality,
                            'speed' => $speed,
                            'ordercontent_create_time' => $time
                        ]);
                    if ($update == true) {
                        DB::table('order')->where('order_id',$order_id)->update(['order_status'=>3]);
                        return json::json('success', '评论成功', '');
                    } else {
                        return json::json('failure', '由于网络或者不正当原因失败', json::value(1));
                    }
                }else{
                    return json::json('failure', '由于网络或者不正当原因失败', json::value(1));
                }
            }
        }
    }

    public function OrderStatus(Request $request,$type){
        if($request ->isMethod('post')){
            $user_id        = $request ->input('user_id');
            if($type ==4){
                $data = DB::table('order')
                    ->leftjoin('goods','order.order_goods_id','=','goods.goods_id')
                    ->where('user_order', $user_id)
                    ->where('order_type',1)
                    ->get();
                if ($data == true) {
                    return json::json('success', '获取商品订单成功', $data);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', json::value(1));
                }
            }else {
                $data = DB::table('order')
                    ->leftjoin('goods','order.order_goods_id','=','goods.goods_id')
                    ->where('user_order', $user_id)
                    ->where('order_status', $type)
                    ->where('order_type',1)
                    ->get();
                if ($data == true) {
                    return json::json('success', '获取商品订单成功', $data);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', json::value(1));
                }
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //生成订单
    public function AddOrder(Request $request){
        if($request ->isMethod('post')){
            $Array          = (object)array();
            $order          = date('Ymdhis').rand(1000,9999);
            $user_id        = $request ->input('user_id');  //用户ID
            $goods_id       = $request ->input('goods_id');
            $order_goodsname = $request ->input('order_goodsname');//商品名称
            $order_address    = $request ->input('order_address'); //地址ID
            $sum              = $request ->input('sum');            //金额
            $quantity         = $request ->input('quantity');      //数量
            $order_status     = 1;
            $order_type       = $request ->input('order_type');
            $time             = date('Y-m-d H:i:s');
            $data             = DB::table('order')
                              ->insert([
                                  'order'   =>$order,
                                  'user_order'  =>$user_id,
                                  'order_goods_id'    => $goods_id,
                                  'order_goodsname'  =>$order_goodsname,
                                  'order_address'    =>$order_address,
                                  'sum'               =>$sum,
                                  'quantity'          =>$quantity,
                                  'order_status'     =>$order_status,
                                  'order_type'       =>$order_type,
                                  'order_create_time'=>$time
                              ]);
            if ($data == true) {
                return json::json('success', '订单生成成功',array('order'=>$order));
            } else {
                return json::json('failure', '订单生成失败', $Array);
            }
        }
    }

    public function OrderID(Request $request){
        if($request ->isMethod('post')){
            $order    = $request ->input('order_id');
            $data     = DB::table('order')
                      ->where('order_id',$order)
                      ->first();
            if($data == true){
                $dd   = array('order'=>$data->order,'order_id'=>$data ->order_id);
                return json::json('success', '订单获取成功',$dd);
            }else{
                $dd  = (object)array();
                return json::json('failure', '订单获取失败', $dd);
            }
        }
    }

    //点击支付时
    public function PayOrder(Request $request){
        if($request ->isMethod('post')){
            $Array          = (object)array();
            $account        = $request ->input('account');   //支付账号
            $manner         = $request ->input('manner');    //支付方式
            $order_id      = $request   ->input('order_id');  //订单ID
            $data          = DB::table('order')
                           ->where('order_id',$order_id)
                           ->update([
                               'account'  =>$account,
                               'manner'   =>$manner
                           ]);
            if ($data == true) {
                return json::json('success', '支付',$Array);
            } else {
                return json::json('failure', '获取订单失败', $Array);
            }

        }
    }

    public function DelOrder(Request $request){
        if($request ->isMethod('post')){
            $Array          = (object)array();
            $id               = $request ->input('order_id');
            $data             = DB::table('order')
                              ->where('order_id',$id)
                              ->where('order_status',1)
                              ->delete();
            if ($data == true) {
                return json::json('success', '删除订单成功',$Array);
            } else {
                return json::json('failure', '删除订单失败', $Array);
            }
        }
    }

   public function urlorder(Request $request){
        if($request ->isMethod('post')){
            $id    = $request ->input('order');
            $datas = DB::table('user')
                   ->leftjoin('order','order.user_order','=','user.user_id')
                   ->where('order_id',$id)
                   ->get();
            return $datas;
        }

   }





}



